package com.veryUsedCar.utils;
 
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
 
/**
 * json序列化工具类 用的jackson包
 */
public class JsonUtil {
 
    public static final ObjectMapper mapper;
 
    public ObjectMapper getMapper() {
        return mapper;
    }
 
    /**
     * 根据自己的需求自定义增删
     */
    static {
        mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        //时间格式
        module.addSerializer(LocalDate.class, new LocalDateSerializer());
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer());
        module.addSerializer(LocalTime.class, new LocalTimeSerializer());
        module.addDeserializer(LocalTime.class, new LocalTimeDeserializer());
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        //浮点格式
        module.addSerializer(BigDecimal.class, new BigDecimalSerializer());
        module.addDeserializer(BigDecimal.class, new BigDecimalDeserializer());
 
        mapper.registerModule(module);
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    }
 
    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException("转换json字符失败!");
        }
    }
 
    public static <T> T toObject(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            throw new RuntimeException("将json字符转换为对象时失败!");
        }
    }
}
 
//时间相关
class LocalDateSerializer extends JsonSerializer<LocalDate> {
 
    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    @Override
    public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeString(dateFormatter.format(value));
    }
}
 
class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return LocalDate.parse(p.getText(), dateFormatter);
    }
}
 
class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
 
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    @Override
    public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeString(dateTimeFormatter.format(value));
    }
 
}
 
class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return LocalDateTime.parse(p.getText(), dateTimeFormatter);
    }
}
 
class LocalTimeSerializer extends JsonSerializer<LocalTime> {
 
    private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
 
    @Override
    public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeString(timeFormatter.format(value));
 
    }
 
}
 
class LocalTimeDeserializer extends JsonDeserializer<LocalTime> {
    private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
 
    @Override
    public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return LocalTime.parse(p.getText(), timeFormatter);
    }
}
 
class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
 
    @Override
    public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
        BigDecimal bigDecimal = value.setScale(2, RoundingMode.DOWN);
        jgen.writeString(bigDecimal.toString());
    }
}
 
class BigDecimalDeserializer extends JsonDeserializer<BigDecimal> {
 
    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        return new BigDecimal(p.getText()).setScale(2, RoundingMode.DOWN);
    }
}
 